#include "streamhandler.h"

using namespace invertedindex;

StreamHandler::StreamHandler():
	srf(),
	bf("./data/bloques.dat",StreamFile::m_read|StreamFile::m_write, srf,2048){
}

StreamHandler::~StreamHandler(){
}

bool StreamHandler::insertDocuments(WordContainer& wc, const DocumentsContainer& dc){
	StreamRegister sr(wc.getWordId());
	sr.setDocumentsContainer(dc);
	BlockPointer* bp = dynamic_cast<BlockPointer*>(bf.write(sr));
	wc.setBP(bp);
	delete bp;
	return true;
}

bool StreamHandler::updateDocuments(WordContainer& wc, const DocumentsContainer& dc){
	StreamRegister sr(wc.getWordId());
	sr.setDocumentsContainer(dc);
	BlockPointer* bpa = wc.getBP();
	BlockPointer* bp = dynamic_cast<BlockPointer*>(bf.update(sr, *bpa));
	wc.setBP(bp);
	delete bp;
	delete bpa;
	return true;
}

bool StreamHandler::searchDocuments(const WordContainer& wc, DocumentsContainer& dc){
	BlockPointer* bp = wc.getBP();
	StreamRegister* sr = dynamic_cast<StreamRegister*>(bf.read(*bp));
	sr->exportDocumentsContainer(dc);
	delete bp;
	delete sr;
	return true;
}

void StreamHandler::toOstream(std::ostream& out){
	bf.toOstream(out);
}
